package logic

import (
	"context"

	"go-weixin/app/music/musicrpc/internal/domain"
	"go-weixin/app/music/musicrpc/internal/svc"
	"go-weixin/app/music/musicrpc/pb/proto/music"

	"github.com/zeromicro/go-zero/core/logx"
)

type MusicListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	musicDomain *domain.MusicDomain
}

func NewMusicListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MusicListLogic {
	return &MusicListLogic{
		ctx:         ctx,
		svcCtx:      svcCtx,
		Logger:      logx.WithContext(ctx),
		musicDomain: domain.NewMusicDomain(*svcCtx.ChenDb),
	}
}

func (l *MusicListLogic) MusicList(in *music.MusicListReq) (*music.MusicListResp, error) {
	musicList, err := l.musicDomain.MusicList(in.Page)
	if err != nil {
		return nil, err
	}
	var resp []*music.MusicBase
	if len(musicList) > 0 {
		for _, m := range musicList {
			resp = append(resp, &music.MusicBase{
				Id:     m.Id,
				Title:  m.Title,
				Artist: m.Artist,
				Path:   m.Path,
				Md5:    m.Md5,
				Year:   m.Year,
				Size:   m.Size,
			})
		}
	}

	return &music.MusicListResp{
		MusicList: resp,
	}, nil
}
